package com.bizzan.bitrade.listenConfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;

@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${spring.application.name:default-value}")
    private String appName;

    @Value("${spring.cloud.client.ipAddress}")
    private String ipAddress;

//    @Value("${spring.application.name}")
//    private String name;

    @Value("${server.port}")
    private String port;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
//        PostConstructMethod，SpringContextHolder，TestController都不能获取属性值，这里可以获得属性值
        // 在容器刷新完成后执行
        // 注意：可能会多次触发，需要判断事件来源
        System.out.println("Spring容器加载完成");
        System.out.println("appName = " + appName);
        System.out.println("ipAddress = " + ipAddress);
//            System.out.println("name = " + name);
        System.out.println("port = " + port);
        if (event.getApplicationContext().getParent() == null) {
            System.out.println("Spring容器加载完成");
            System.out.println("appName = " + appName);
            System.out.println("ipAddress = " + ipAddress);
//            System.out.println("name = " + name);
            System.out.println("port = " + port);
        }
    }
}